package cn.com.mr01.lianxi.mr_lianxi_05;

import java.io.IOException;
import java.util.Iterator;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

public class IntSumReducer extends
		Reducer<Text, IntWritable, Text, IntWritable> {

	// reduce端 : shuffle(copy | sort and merge | group) --- reduce(k, values)
	// 有多少个key就group多少个组

	IntWritable count = new IntWritable();

	@Override
	protected void reduce(Text key, Iterable<IntWritable> values,
			Context context) throws IOException, InterruptedException {

		// 1. 统计结果
		int cnt = 0;
		Iterator<IntWritable> it = values.iterator();
		while (it.hasNext()) {
			cnt += it.next().get();
		}
		count.set(cnt);
		// 2. <k,v>输出
		context.write(key, count);
	}
}
